/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wap.rockvilletech.util.imaging;

/**
 * 20-01-2009
 * @author Bilal
 */
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import java.net.URL;

import com.gif4j.Watermark;
import com.gif4j.TextPainter;

import java.util.HashMap;
import org.apache.log4j.Logger;

public class ProcessUCard {

    private static final Logger log = Logger.getLogger(ProcessUCard.class);
    final static int LEFT = 0;
    final static int RIGHT = 1;
    final static int CENTER = 2;
    final static int EQUALITY = 3;

    public ProcessUCard() {
    }// End ProcessUCard

    public static void reloadConfig() {
    }

    //--String strPath, String strUpTxt, String strDownTxt, int w, int h
    /**
     * <pre>
     * HashMap must contain the following parameters
     *  - imgPath
     *  - recvName
     *  - sndrName
     *  - w
     *  - h
     *  - Point1
     *  - Point2
     *  - Bias
     *  - FillColor
     *  - water_mark
     * <i>Note</i>: All the parameters are strings
     * </pre>
     * @param hm
     * @return
     */
    public BufferedImage processCard(HashMap hm) {

        String strPath = hm.get("imgPath").toString();
        String strUpTxt = hm.get("recvName").toString();
        String strDownTxt = hm.get("sndrName").toString();
        String P1 = hm.get("Point1").toString();
        String P2 = hm.get("Point2").toString();
        String Bias = hm.get("Bias").toString();
        String Fillcolor = hm.get("FillColor").toString();
        String waterMark_txt = "";
        boolean doWaterMarking = false;
        int w = Integer.parseInt(hm.get("w").toString());
        int h = Integer.parseInt(hm.get("h").toString());
        BufferedImage image = null;
        BufferedImage centerImage = null;//
        BufferedImage imageTemp = null;//Return final image
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int intTotalWidth = 0, intTotalHeight = 0;

        BufferedImage imageReceiverName = null;//Sender
        BufferedImage imageSenderName = null;//To
        BufferedImage fullImage = null;//final
        int upTxtHeight = 0, dwnTxtHeight = 0;

        try {
            waterMark_txt = (String)hm.get("water_mark");
            if (waterMark_txt == null) {
            } else {
                waterMark_txt = waterMark_txt.trim();
                if (waterMark_txt.length() > 0) {
                    doWaterMarking = true;
                }
            }
        } catch (Exception waterMarkException) {
            log.error("Unable to get Water Mark Info.",waterMarkException);
        }

        try {
            TextPainter tp_ReceiverName = new TextPainter(new java.awt.Font("Verdena", java.awt.Font.BOLD, 16), java.awt.Color.WHITE, java.awt.Color.WHITE, java.awt.Color.BLACK, false);
            tp_ReceiverName.setOutlinePaint(new java.awt.Color(152, 186, 65));
            tp_ReceiverName.setBackgroundColor(new java.awt.Color(251, 143, 32));

            TextPainter tp_SenderName = new TextPainter(new java.awt.Font("Verdena", java.awt.Font.BOLD, 16), java.awt.Color.WHITE, java.awt.Color.WHITE, java.awt.Color.BLACK, false);
            tp_SenderName.setOutlinePaint(new java.awt.Color(152, 186, 65));
            tp_SenderName.setBackgroundColor(new java.awt.Color(251, 143, 32));

            //tp_ReceiverName.setAntialiased(true);

            imageReceiverName = tp_ReceiverName.renderText(strUpTxt, w, false, true);
            upTxtHeight = imageReceiverName.getHeight();
            log.debug("upTxtHeight : " + upTxtHeight);

            //System.out.println("imageReceiverName :" + imageReceiverName +" Height "+imageReceiverName.getHeight()+" width"+imageReceiverName.getWidth());
            imageSenderName = tp_SenderName.renderText(strDownTxt, w, false, true);
            dwnTxtHeight = imageSenderName.getHeight();

            log.debug("dwnTxtHeight : " + dwnTxtHeight);


            h = h - (upTxtHeight + dwnTxtHeight);

        } catch (Exception txtPainterException) {
            log.error(" txtPainterException :" + txtPainterException);


        }//end text painter



        try {

            image = (BufferedImage) ProcessWallpaper.generateWallpaper(strPath, P1, P2, Bias, Fillcolor, Integer.toString(w), Integer.toString(h));

            if (doWaterMarking) {
                log.debug("Apply '" + waterMark_txt + "' as a Water Mark");

                int waterMarkFontSize = 14;

                if (w > 500) {
                    waterMarkFontSize = 72;
                } else if (w > 400) {
                    waterMarkFontSize = 48;
                } else if (w > 300) {
                    waterMarkFontSize = 36;
                } else if (w > 200) {
                    waterMarkFontSize = 24;
                }

                //create new TextPainter
                TextPainter textPainter = new TextPainter(new Font("Verdana", Font.BOLD, waterMarkFontSize));
                textPainter.setOutlinePaint(Color.WHITE);
                //render the specified text outlined
                BufferedImage renderedWatermarkText = textPainter.renderString(waterMark_txt, true);
                //create new Watermark
                Watermark watermark = new Watermark(renderedWatermarkText, Watermark.LAYOUT_MIDDLE_CENTER);
                //apply watermark to the specified image and return the result
                image = watermark.apply(image);
            }

            //--image = ImageIO.read(new File(strPath));
        } catch (Exception ex) {
            log.error("FATAL: Unable to find Image:"+strPath+"=",ex);
        }

        try {

            if (w >= image.getWidth()) {
                w = image.getWidth();
            }

            /*--            ParameterBlock pb = new ParameterBlock();
            pb.addSource(image);
            pb.add(((float) w / image.getWidth()));
            pb.add(((float) h / image.getHeight()));
            //pb.add(1.0F);
            //pb.add(1.0F);
            pb.add(0.0F);
            pb.add(0.0F);
            pb.add(Interpolation.getInstance(Interpolation.INTERP_BILINEAR));
            centerImage = JAI.create("scale", pb).getAsBufferedImage();
            --*/
            centerImage = (BufferedImage) image;
            image = null;

            intTotalWidth = centerImage.getWidth();
            intTotalHeight = centerImage.getHeight();

            if (imageReceiverName != null) {
                intTotalHeight = intTotalHeight + upTxtHeight;
            }
            if (imageSenderName != null) {
                intTotalHeight = intTotalHeight + dwnTxtHeight;
            }

            fullImage = new BufferedImage(intTotalWidth, intTotalHeight, BufferedImage.TYPE_INT_RGB);

            java.awt.Graphics graphicsFullImage = fullImage.getGraphics();

            graphicsFullImage.drawImage(imageReceiverName, 0, 0, null);
//change  Sender name to receiver name
            graphicsFullImage.drawImage(centerImage, 0, upTxtHeight, null);
/// centerImage.getHeight() + imageReceiverName.getHeight()
            graphicsFullImage.drawImage(imageSenderName, 0, centerImage.getHeight() + upTxtHeight, null);

        } catch (Exception e) {
            log.error(e);
        }

        return fullImage;
        // return imageReturn;
    }

}
